/**
 * Tree store for nested set tree representation.
 */
Ext.ux.maximgb.treegrid.NestedSetStore = Ext.extend(Ext.ux.maximgb.treegrid.AbstractTreeStore,
{
	/**
	 * @cfg {String} left_field_name Record NS-left bound field name.
	 */
	left_field_name : '_lft',
	
	/**
	 * @cfg {String} right_field_name Record NS-right bound field name.
	 */
	right_field_name : '_rgt',
	
	/**
	 * @cfg {String} level_field_name Record NS-level field name.
	 */
	level_field_name : '_level',
	
	/**
	 * @cfg {Number} root_node_level Root node level.
	 */
	root_node_level : 1,
	
	getRootNodes : function()
	{
		var i, 
				len, 
				result = [], 
				records = this.data.getRange();
		
		for (i = 0, len = records.length; i < len; i++) {
			if (records[i].get(this.level_field_name) == this.root_node_level) {
				result.push(records[i]);
			}
		}
		
		return result;
	},
	
	getNodeDepth : function(rc)
	{
		return rc.get(this.level_field_name) - this.root_node_level;
	},
	
	getNodeParent : function(rc)
	{
		var result = null,
				rec, records = this.data.getRange(),
				i, len,
				lft, r_lft,
				rgt, r_rgt,
				level, r_level;
				
		lft = rc.get(this.left_field_name);
		rgt = rc.get(this.right_field_name);
		level = rc.get(this.level_field_name);
		
		for (i = 0, len = records.length; i < len; i++) {
			rec = records[i];
			r_lft = rec.get(this.left_field_name);
			r_rgt = rec.get(this.right_field_name);
			r_level = rec.get(this.level_field_name);
			
			if (
				r_level == level - 1 &&
				r_lft < lft &&
				r_rgt > rgt
			) {
				result = rec;
				break;
			}
		}
		
		return result;
	},
	
	getNodeChildren : function(rc)
	{
		var lft, r_lft,
				rgt, r_rgt,
				level, r_level,
				records, rec,
				result = [];
				
		records = this.data.getRange();
		
		lft = rc.get(this.left_field_name);
		rgt = rc.get(this.right_field_name);
		level = rc.get(this.level_field_name);
		
		for (i = 0, len = records.length; i < len; i++) {
			rec = records[i];
			r_lft = rec.get(this.left_field_name);
			r_rgt = rec.get(this.right_field_name);
			r_level = rec.get(this.level_field_name);
			
			if (
				r_level == level + 1 &&
				r_lft > lft &&
				r_rgt < rgt
			) {
				result.push(rec);
			}
		}
		
		return result;
	}
});